<template>
	<view style="width: 100%;">
		<view class="detail">
			<view class="detail__content">
				<view class="detail__content__title"><text class="detail__content__title__text">{{detail.newsTitle}}</text></view>
				<view class="detail__content__box"><u-parse class="detail__content__box__info" :content="detail.newsContent"></u-parse></view>
				<view class="detail__content__bottom">
					<text class="detail__content__bottom__source">{{detail.newsSource}}</text>
					<text class="detail__content__bottom__time">{{detail.createTime}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uParse from "components/feng-parse/parse"
	
	export default {
		data() {
			return {
				detail: null, //详情
			}
		},
		components: {
			uParse,
		},
		onLoad(e) {
			let self = this
			self.detail = JSON.parse(decodeURIComponent(e.item))
			console.log(e.type);
			self.detail.newsContent = this.formatRichText(self.detail.newsContent)
			// 新闻已读
			self.$api.fAppNewsReadNews({
				id: self.detail.id
			}).then((res) => {
				// console.log(res);
				if(e.type == 1) {
					let pages = getCurrentPages();
					let page = pages[pages.length - 2];
					page.$vm.news_lists[e.index].newsStatus = 1
				}
				uni.$emit('news_info')
			}).catch(e=>{
				console.log(e);
			})
		},
		methods: {
			/**
			 * @name 放大图
			 * @param {Object} img img地址
			 */
			previewImage(img) {
				uni.previewImage({
					current: img, // 当前显示图片的http链接
					urls: [img] // 需要预览的图片http链接列表
				});
			},
			formatRichText (html) { //控制小程序中图片大小
				let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
				  match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
				  match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
				  match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
				  return match;
				});
				newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
				  match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
				  return match;
				});
				newContent = newContent.replace(/<br[^>]*\/>/gi, '');
				newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;width:100%;height:auto;display:inline-block;margin:10rpx auto;width:100%"');
				return newContent;
			}

		}
	}
</script>

<style lang="less">
	.detail {
		width: 93.6%;padding: 12rpx 22rpx;display: flex;align-items: center;border-radius: 14rpx;
		&__content {
			width: 100%;display: flex;flex-direction: column;justify-content: space-between;
			&__title {
				width: 100%;
				&__text {
					font-size: 36rpx;font-weight: 700;color: #333333;
				}
			}
			&__box {
				width: 100%;
				&__info {
					font-size: 24rpx;color: #333333; width: 100%;
				}
			}
			&__bottom {
				margin-top: 16rpx;
				&__source {
					font-size: 24rpx;color: rgba(49,114,182,1);
				}
				&__time {
					font-size: 24rpx;color: rgba(153,153,153,1);margin-left: 32rpx;
				}
			}
		}
	}

</style>
